(function() {
	require(["Cookies"], function(Cookies) {
		App.set("$Cache.sign_in.name", Cookies.get("cache_user_login_name"));
		// 切换登录者
		App.set("$Event.sign_in.changloginType", function() {
			App.model.toggle("$Cache.sign_in.isBusLogin");
			if (App.get('$Cache.sign_in.isBusLogin')) {
				App.set("$Cache.sign_in.logintype", 'bus');
			} else {
				App.set("$Cache.sign_in.logintype", 'user');
			};
		});
		App.set('$Event.sign_in.changlogin', function() {
			var logintype = App.get("$Cache.sign_in.logintype");
			if (logintype == "bus") { //商家
				App.set("$Cache.sign_in.name", Cookies.get("cache_bus_login_name"));
			} else if (logintype == "user") { //买家
				App.set("$Cache.sign_in.name", Cookies.get("cache_user_login_name"));
			}
		});

		//默认是用户登录
		App.set("$Cache.sign_in.logintype", "user");
		//切换到用户注册
		App.set("$Event.sign_in.ChangeSign", function() {
			App.model.toggle('$Cache.sign_in.isUp');
		})
		if (Path.getQuery("id") == "2") {
			App.get("$Event.sign_in.ChangeSign")();
		} else if (Path.getQuery("id") == "3") { //切换到商家登录
			App.set("$Cache.sign_in.logintype", "bus");
			App.get('$Event.sign_in.changlogin')();
		}
		/*
		 *登录
		 */

		App.set("$Event.sign_in.common_login", function() {
			var logintype = App.get("$Cache.sign_in.logintype");
			if (logintype == "bus") { //商家
				App.get("$Event.sign_in.admin_login")();
			} else if (logintype == "user") { //买家
				App.get("$Event.sign_in.login")();
			}
		});

		//获取一个验证码
		function _loadCodeImg() {
			// $code.val("");
			// $signInImg.prop("src",appConfig.user.sign_in_img_code_url+"?_="+Math.random());
		};
		_loadCodeImg();

		App.set("$Event.sign_in.login", function() {
			var login_data = App.get("$Cache.sign_in");
			coAjax.post(appConfig.user.sign_in_url, login_data, function success(result) {
				alert("success", "登录成功");

				App.set("loginer", result.result);
				Cookies.set("cache_user_login_name", login_data.name);
				var callbackUrl = Path.getQuery("cb_url");
				if (callbackUrl) {
					Path.jump(callbackUrl);
				} else {
					//到首页
					Path.jump("/main.html");
				}
				eventManager.fire("getLoginer");

			}, function error(errorCode, xhr, errorMsg) {
				alert("error", errorMsg);
				_loadCodeImg();
			});
		});

		// 卖家
		App.set("$Event.sign_in.admin_login", function(argument) {
			var username = App.get("$Cache.sign_in.name");
			var password = App.get("$Cache.sign_in.password");
			coAjax.post(appConfig.bus.login, {
				name: username,
				password: password
			}, function() {
				alert("success", "商家登录成功");
				var callbackUrl = Path.getQuery("cb_url");
				Cookies.set("cache_bus_login_name", username);
				var callbackUrl = Path.getQuery("cb_url");
				//直接到商家后台
				Path.jump("http://admin.dotnar.com");
			}, function(errorCode, xhr, errorMsg) {
				alert("error", errorMsg);
			})
		});
		App.set("$Event.sign_in.to_password_input", function() {
			App.set("$Cache.sign_in.is_focus", true);
			App.set("$Cache.sign_in.is_focus", false);
		});


		/*
		 * 注册
		 */
		App.set("$Event.sign_up.register", function() {
			if (!App.get('$Cache.sign_up.name')) {
				alert('warn', '请输入用户名')
				return;
			};
			if (!App.get('$Cache.sign_up.mobile_phone')) {
				alert('warn', '请输入手机号')
				return;
			};
			if (!App.get('$Cache.sign_up.email')) {
				alert('warn', '请输入邮箱')
				return;
			};
			if (!App.get('$Cache.sign_up.password')) {
				alert('warn', '请输入密码')
				return;
			};
			if (!App.get('$Cache.sign_up.confirm_password')) {
				alert('warn', '请再次输入密码')
				return;
			};
			if (!App.get('$Cache.sign_up.phone_code')) {
				alert('warn', '请输入验证码')
				return;
			};
			var register_data = App.get("$Cache.sign_up");
			//邀请注册
			register_data.recommender_id = Cookies.get("recommender_id");
			coAjax.post(appConfig.user.sign_up_url, register_data, function(result) {
				alert("success", "注册成功");
				App.set("$Cache.sign_in.name", register_data.name);
				//清空表单
				App.set("$Cache.sign_up", {});
				//切换到登录
				App.get("$Event.sign_up.to_sign_in")();
			}, function(errorCode, xhr, errorMsg) {
				alert("error", errorMsg);
				//重新获取验证码
				App.set("$Cache.sign_up.wite_server", false);
			});

		});
		var _ti;

		function _phone_code_time(argument) {
			clearInterval(_ti);
			var _s = 60;
			_ti = setInterval(function() {
				_s -= 1;
				App.set("$Cache.sign_up.next_phone_code_time", _s);
				if (_s === 0) {
					clearInterval(_ti);
					App.set("$Cache.sign_up.wite_server", false);
				}
			}, 1000);
		}
		App.set("$Event.sign_up.checkedMobilePhone", function() {
			coAjax.get(appConfig.user.registerCheckedMobilePhone + App.get("$Cache.sign_up.mobile_phone"), function() {
				alert("success", "手机号码可用");
			});
		});
		App.set("$Event.sign_up.checkedEmail", function() {
			coAjax.get(appConfig.user.registerCheckedEmail + App.get("$Cache.sign_up.email"), function() {
				alert("success", "邮箱可用");
			});
		});
		App.set("$Event.sign_in.findpaw", function() {
			Path.jump("http://www.dotnar.com/find_paw.html?cb=" + location.host);
		});
		App.set("$Event.sign_up.change_user_image_code", function() {
			App.set("$Cache.sign_up.image_code_random", Math.random());
			App.set("$Cache.sign_up.image_code_for_phone_code", "");
		});
		App.set("$Event.sign_up.get_register_code_with_image_code", function() {
			App.set("$Cache.sign_up.wite_server", true);
			App.set("$Cache.sign_up.__input_phone_code", true);
			coAjax.get(appConfig.user.register_code_with_image_code + App.get("$Cache.sign_up.image_code_for_phone_code"), {
				mobile_phone: App.get("$Cache.sign_up.mobile_phone")
			}, function(result) {
				alert(result.result);
				_phone_code_time();
			}, function(errorCode, xhr, errorMsg) {
				alert("error", errorMsg);
				App.set("$Cache.sign_up.wite_server", false);
				App.set("$Cache.sign_up.__input_phone_code", false);
				//重新获取图形验证码
				App.get("$Event.sign_up.change_user_image_code")();
			});
		});
		App.set("$Event.sign_up.reset_register_code", function() {
			App.set("$Cache.sign_up.__input_phone_code", false);
			App.get("$Event.sign_up.change_user_image_code")();
		});
	});
}());